package com.puppet.building.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.puppet.building.pojo.Building;
import com.puppet.building.pojo.MyPageData;
import com.puppet.building.pojo.Result;
import com.puppet.building.service.BuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/building")
public class BuildingController {

    @Autowired
    private BuildingService buildingService;

    @GetMapping({"/{pageNum}/{pageSize}/{search}","/{pageNum}/{pageSize}"})
    public Result<?> getAllBuildings(@PathVariable Integer pageNum, @PathVariable Integer pageSize,@PathVariable(required = false) String search) {
        //1.开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Building> allBuildings = buildingService.getAllBuildings(search);
        PageInfo<Building> pageInfo = new PageInfo<>(allBuildings);
        MyPageData myPageData = new MyPageData(pageInfo.getList(), pageInfo.getTotal());
        return Result.ok(myPageData);
    }

    @PostMapping
    public Result<?> addNewBuilding(@RequestBody Building building) {
        return Result.ok(buildingService.addNewBuilding(building),"保存成功");
    }

    @DeleteMapping("/{buildingId}")
    public Result<?> deleteBuilding(@PathVariable Integer buildingId){
        return Result.ok(buildingService.deleteBuilding(buildingId),"删除成功");
    }

    @PostMapping("/deleteBuildings")
    public Result<?> deleteBuildings(@RequestBody List<Integer> ids){
        return Result.ok(buildingService.deleteBuildings(ids),"删除成功");
    }

    @GetMapping("/{buildingId}")
    public Result<?> getBuildingById(@PathVariable Integer buildingId){
        return Result.ok(buildingService.getBuildingById(buildingId));
    }

    @PutMapping
    public Result<?> updateBuilding(@RequestBody Building building){
        return Result.ok(buildingService.updateBuilding(building),"修改成功");
}
}
